﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
// Import model classes!
using UTC_Project_Manager.Model.Infrastructure;

namespace UTC_Project_Manager.Controls
{
    /// <summary>
    /// Interaction logic for notesControl.xaml
    /// </summary>
    public partial class NotesControl : UserControl
    {
        // Global index of the current selected note
        private int index = -1;
        // Is the edition of a note available?
        private bool editable = true;
        
       

        private delegate void focus(TextBox addTextBox);


        public NotesControl()
        {
            InitializeComponent();
            init();
        }

       

        private void addNoteButton_Click(object sender, RoutedEventArgs e)
        {
            // Disable the MouseDoubleClick event
            editable = false;
            TextBox addTextBox = new TextBox();
            addTextBox.KeyDown += new KeyEventHandler(addTextBox_KeyDown);
            addTextBox.Width = this.ActualWidth - 20;
            //addTextBox.AcceptsReturn = true;
            addTextBox.TextWrapping = TextWrapping.Wrap;
            this.notesListView.Items.Add(addTextBox);
            // In this case it's necessary to use a Dispatcher to handle the focus event
            this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.SystemIdle,
                new focus(setTextBoxFocus), addTextBox);
            // Enable the removeNoteButton
            
        }

        private void init()
        {
            if (App.MyConfig.OurProject.Notes != null)
                foreach (Note note in App.MyConfig.OurProject.Notes)
                {
                    TextBlock tb = new TextBlock();
                    tb.Width = 230;
                    tb.Text = ". " + note.Description;
                    tb.TextWrapping = TextWrapping.Wrap;
                    this.notesListView.Items.Add(tb);
                }
        }

        // Set the focus to the current TextBox
        private void setTextBoxFocus(TextBox addTextBox) {
            addTextBox.Focus();
        }

        // Every time the note is larger than 25 characters display only the first 25 plus ' ...'
        // When the user double clicks on it, it will display the hole note.
        // Ex: "This is a very large note ..."
        private void addTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (App.MyConfig.OurProject.Notes == null)
                App.MyConfig.OurProject.Notes = new List<Note>();

            if (e.Key == Key.Return)
            {
                String noteString = ((TextBox)sender).Text;
                int pos = 0;
                // We are not editing!
                if (index == -1) {
                    // Current position is the last position of the list (Items.Count - 1)
                    pos = this.notesListView.Items.Count - 1;
                    // Adds the new note to the 'notesList' object
                    App.MyConfig.OurProject.Notes.Add(new Note(noteString, DateTime.Now));
                }
                // We are editing!
                else
                {
                    // Current position is 'index' (Selected item)
                    pos = index;
                    // Change the description and date of the note
                    Note auxNote = App.MyConfig.OurProject.Notes.ElementAt(index);
                    auxNote.Description = noteString;
                    auxNote.Date = DateTime.Now;
                    App.MyConfig.OurProject.Notes.RemoveAt(index);
                    App.MyConfig.OurProject.Notes.Insert(index, auxNote);
                    // Reset the global 'index'
                    index = -1;
                }
                // Remove TextBox at position 'pos'
                this.notesListView.Items.RemoveAt(pos);
                // If is a large note, we cut some text (the original will be stored in the 'noteList' object)
                //if (noteString.Length >= 25) {
                //    noteString = noteString.Substring(0, 25);
                //    noteString += " ...";
                //}
                TextBlock tb = new TextBlock();
                tb.Width = this.ActualWidth - 20;
                tb.Text = ". " + noteString;
                tb.TextWrapping = TextWrapping.Wrap;
                // Insert 'noteString' at position 'index'
                this.notesListView.Items.Insert(pos, tb);
                // Enable edition
                editable = true;
           }
        }

        // Remove a note
        private void removeNoteButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.notesListView.SelectedItem != null)
            {
                // Remove the note from 'notesList' object
                App.MyConfig.OurProject.Notes.RemoveAt(this.notesListView.SelectedIndex);
                // Remove the note string from the ListView
                this.notesListView.Items.Remove(this.notesListView.SelectedItem);
                if (this.notesListView.Items.Count == 0)
                    this.removeNoteButton.IsEnabled = false;
            }
        }

        // Edit a note
        private void notesListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (this.notesListView.SelectedIndex != -1)
            {
                if (editable)
                {
                    // Get the current note description from the 'noteList' object
                    String oldNoteString = App.MyConfig.OurProject.Notes.ElementAt(this.notesListView.SelectedIndex).Description;
                    // Create a new TextBox
                    TextBox noteTextBox = new TextBox();
                    // Set the current note to the TextBox's text
                    noteTextBox.Text = oldNoteString;
                    // Handle the event
                    noteTextBox.KeyDown += new KeyEventHandler(addTextBox_KeyDown);
                    noteTextBox.Width = this.ActualWidth - 20;
                    noteTextBox.TextWrapping = TextWrapping.Wrap;
                    // Get the SelectedIndex's position
                    int pos = this.notesListView.SelectedIndex;
                    // Updates the global 'index'
                    index = pos;
                    // Remove the old object (String) at pos
                    this.notesListView.Items.RemoveAt(pos);
                    // Insert the new object (TextBox) at pos
                    this.notesListView.Items.Insert(pos, noteTextBox);
                    // We don't want to handle the MouseDoubleClick event if we haven't finished yet the edition :B
                    editable = false;
                }
            }
        }

        private void notesListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.notesListView.SelectedIndex != -1)
                this.removeNoteButton.IsEnabled = true;
            else
                this.removeNoteButton.IsEnabled = false;
        }
    }
}